
/**
 * \class searchParametersObject
 * \brief this class handles the mapping functionality
 * \param eventID The interger to uniquely identify the event
 * \param eventName The string to hold the event Name
 * \param eventType The string to hold the event type
 * \author Mahlatse Maraba
 *
 */

var vacinityEventsArray = new Array();
var markersArray = [];
var arrayOfSearchParameters = new Array();



function searchParametersObject(eventID,eventName,eventType)
{
    this.id = eventID;
    this.label = eventName;
    this.category = eventType;
}


function myVacinityEvents(eventId,eventName,eventLat,eventLong,eventAddress)
{
    this.vevent_id = eventId;
    this.vevent_name= eventName;
    this.vevent_lat= eventLat;
    this.vevent_long = eventLong;
    this.vevent_address =eventAddress;
}

function getVacinityEvents(eventLat,eventLong)
{

    sendRequest.setAction('getVacinityEvents');
    eventCoordinates = {
        'eventLat':eventLat,
        'eventLong':eventLong
    };
    sendRequest.setSendParameters(eventCoordinates);


    $.ajax({
        url: sendRequest.serverURL,
        async: false,
        dataType: 'json',
        data: sendRequest.getSendData(),
        success: function(data) {

            $.each(data, function(key, data)
            {
                var t = new myVacinityEvents(data.event_id,data.event_name,data.event_loc_lat,data.event_loc_long,data.event_address);
                vacinityEventsArray[vacinityEventsArray.length] = t;

                if (data.event_type != null)
                {
                    eventTypeString = data.event_type;
                    var tempEventTypeArray = eventTypeString.split(',');
                    for (i =0 ; i <tempEventTypeArray.length; i++)
                    {
                        arrayOfSearchParameters[arrayOfSearchParameters.length] = new searchParametersObject(data.event_id,data.event_name,tempEventTypeArray[i]);
                    }

                }

            });

        }
    });
}

var gmarkers = [];
var unfilDist = [];
var filDist = [];
var filMarkers = [];
var userCoordinatePnt = null;
var maps;
var userMarkerOptions;

function createMarker(point,name,html,increment) {

    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        //    marker.openInfoWindowHtml("htmldf");
        //$("#dialog-activity-lister").dialog('open');
        openSelectedEvent(name)
    });
    gmarkers[increment] = marker;
    gmarkers[increment].label = name;
    //  i++;
    return marker;
}

function initializeMainMap(){

    // create the map
    maps = new GMap2(document.getElementById("main_map_canvas"));
    //   directions = new GDirections(maps);
    maps.addControl(new GLargeMapControl());
    maps.addControl(new GMapTypeControl());
    maps.addControl(new GScaleControl());

    // Create our "tiny" marker icon
    // Create our "tiny" marker icon
    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
    blueIcon.iconSize = new GSize(35, 34);
    blueIcon.shadowSize = new GSize(45, 45);
    userMarkerOptions = {
        icon:blueIcon
    };

    if (coordinateFinder.userLat != null && coordinateFinder.userLong !=null)
    {
        maps.setCenter(new GLatLng(coordinateFinder.userLat,coordinateFinder.userLong), 12);
        userCoordinatePnt = new GLatLng(coordinateFinder.userLat,coordinateFinder.userLong);
        marker = new GMarker(userCoordinatePnt);
        gmarkers[0] = marker;
        maps.addOverlay(new GMarker(userCoordinatePnt, userMarkerOptions));
    }
    else

    {
        maps.setCenter(new GLatLng(-26,28), 12);
    }
    for (var i = 0; i < vacinityEventsArray.length; i++) {
        var lat = vacinityEventsArray[i].vevent_lat;
        var lng = vacinityEventsArray[i].vevent_long;

        var point = new GLatLng(lat,lng);
        var label = vacinityEventsArray[i].vevent_id;
        var html = vacinityEventsArray[i].vevent_address;

        var marker = createMarker(point,label,html,parseInt(i+1));
        maps.addOverlay(marker);

    }




}


function getFilteredMarkersByDistance(currentDistance)
{
    unfilDist.length =0;
    filDist.length = 0;
    filMarkers.length = 0;
           
    for (var x=0; x<gmarkers.length; x++)
    {
        var d=gmarkers[x].getPoint().distanceFrom(gmarkers[0].getPoint())/1000;
        unfilDist[x] = d;
    }

    var y = 0
    for (var x=0; x<gmarkers.length; x++)
    {

        if(parseInt(unfilDist[x])<= parseInt(currentDistance))
        {
				
            filDist[y] = unfilDist[x];
            filMarkers[y] = gmarkers[x];
            y++;
        }
    }
  
}

function filterEvents()
{
    maps.clearOverlays();

    for (var x=0; x<filMarkers.length; x++)
    {

        if (x == 0)
        {
            maps.addOverlay(new GMarker(userCoordinatePnt, userMarkerOptions));
        }
        else
        {
            maps.addOverlay(filMarkers[x]);
        }					
    }
}


function openSelectedEvent(eventID)
{
    sendRequest.setAction('getDetailsForEvent');
    eventIDInfo = {
        'eventID':eventID
    };
    sendRequest.setSendParameters(eventIDInfo);
    $('#detailedEventInfo').load(sendRequest.serverURL, sendRequest.getSendData(), function(){
        $('#detailedEventInfo').unbind();
        $( "#selectableEventTypeGrid" ).selectable();
        $("#dialog-activity-lister").dialog({
            resizable: true,
            width: 940,
            height: 840,
            modal: true,
            show: 'clip',
            hide: 'clip',
            autoOpen: false,
            closeOnEscape: true,
            open: function(){
                $( "#actvityDetailsAccordion" ).accordion({
                    event: "mouseover",
                    autoHeight: true
                });
                eventLat = $("#eventLatDisplayer").val();
                eventLong = $("#eventLongDisplayer").val();
                //   eventAddress = $("#eventAddressDisplayer").val();
                var point = new GLatLng(eventLat,eventLong);
                var marker = new GMarker(point);
                currentEventGMarkers[currentEventGMarkers.length] = marker;
                //        currentEventGMarkers[currentEventGMarkers.length].label = "Selected Event";
                directionsMap.addOverlay(marker);
                getDirections();
            },
            buttons: {
                'OK': function() {
                    currentEventGMarkers.splice(1,(currentEventGMarkers.length-1));
                    $(this).dialog('close');
                }
            }
        });


        $("#dialog-activity-lister").dialog('open');
    });
}

